/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-10-25
 * Time: 20:06
 */

//和可被 K 整除的子数组
public class Demo1 {

        public int subArraysDivByK(int[] nums, int k) {
            Map<Integer,Integer> hash = new HashMap<>();

            hash.put(0 % k,1);
            int ret = 0, sum = 0;
            for(Integer x : nums) {
                sum += x;

                int r = (sum % k + k) % k;

                ret += hash.getOrDefault(r,0);

                hash.put(r,hash.getOrDefault(r,0)+1);
            }

            return ret;
        }
}
